package com.mike.billingserver.controller.v1;

import com.mike.billingserver.form.UserForm;
import com.mike.billingserver.service.UserService;
import common.lib.core.ResultVO;
import common.lib.core.annotation.NotNeedLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author xiaozefeng
 * @date 2018-12-18 16:19
 */
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping
    @NotNeedLogin
    public ResultVO createUser(@RequestBody @Valid UserForm userForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultVO.error(bindingResult.getFieldError().getDefaultMessage());
        }
        UserForm result = userService.createUser(userForm);
        return ResultVO.ok(result);
    }

}
